import { Component, input, inject, effect, output } from '@angular/core';

import { debug } from '../../utils/logger';

import { validateSettings } from '../../utils/settings_addons';
import { NotifyService, KeybinderService, StorageService } from '../../services';
import { KeyboardShortcutCategory } from '../../services/keybinder/keybinder.service';
import { SettingsState } from 'altair-graphql-core/build/types/state/settings.interfaces';
import { AltairConfig } from 'altair-graphql-core/build/config';
import { Extension } from '@codemirror/state';
import settingsSchema from 'altair-graphql-core/build/typegen/settings.schema.json';
import { getEditorExtensions } from './extensions';
import { IDictionary } from 'altair-graphql-core/build/types/shared';
import { JSONSchema6 } from 'json-schema';

@Component({
  selector: 'app-settings-dialog',
  templateUrl: './settings-dialog.component.html',
  styleUrls: ['./settings-dialog.component.scss'],
  standalone: false,
})
export class SettingsDialogComponent {
  private notifyService = inject(NotifyService);
  private keybinderService = inject(KeybinderService);
  private storageService = inject(StorageService);
  private altairConfig = inject(AltairConfig);

  readonly settings = input<SettingsState>();
  readonly appVersion = input('');
  readonly showSettingsDialog = input(false);
  readonly toggleDialogChange = output<boolean>();
  readonly settingsJsonChange = output<string>();

  themes = this.altairConfig.themes;
  languages = Object.entries(this.altairConfig.languages);
  shortcutCategories: KeyboardShortcutCategory[] = [];
  settingsSchema = settingsSchema as JSONSchema6;
  showForm = true;

  jsonSettings = '';
  localSettings = null;

  editorExtensions: Extension[] = getEditorExtensions();

  constructor() {
    this.shortcutCategories = this.keybinderService.getShortcuts();

    effect(() => {
      const currentSettings = this.settings();
      if (currentSettings) {
        this.updateLocalSettings(JSON.stringify(currentSettings, null, 2));
      }
    });
  }

  onSettingsChange(settingsStr: string) {
    this.updateLocalSettings(settingsStr);
  }

  saveSettings() {
    if (validateSettings(this.jsonSettings)) {
      this.settingsJsonChange.emit(this.jsonSettings);
      this.toggleDialogChange.emit(false);
    } else {
      this.notifyService.error('Check that the settings are correct.');
    }
  }

  onFormDataChange(data: IDictionary) {
    debug.log(data);
    this.onSettingsChange(JSON.stringify(data, null, 2));
  }

  updateLocalSettings(settingsStr: string) {
    this.jsonSettings = settingsStr;
    try {
      this.localSettings = JSON.parse(this.jsonSettings);
    } catch (error) {
      //
    }
  }

  onToggleView() {
    this.showForm = !this.showForm;
  }

  onResetApplicationData(e: Event) {
    e.preventDefault();
    e.stopPropagation();
    if (
      confirm(
        `
    ❌❌❌
    Warning! You are about to reset the application.
    Are you sure you want to reset the application?

    This is not reversible!`
          .trim()
          .replace(/ +/g, ' ')
      )
    ) {
      if (
        confirm(
          `
        THIS IS YOUR LAST WARNING!
        TURN BACK NOW IF YOU DON'T WANT TO LOSE ALL THE APPLICATION DATA.

        ARE YOU REALLY SURE YOU WANT TO RESET ALTAIR?!
      `
            .trim()
            .replace(/ +/g, ' ')
        )
      ) {
        this.storageService.clearAllLocalData();
        location.reload();
      }
    }

    return false;
  }

  trackByIndex(index: number, s: KeyboardShortcutCategory) {
    return index;
  }
}
